// MyLuaVM.cpp: implementation of the CMyLuaVM class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "LUAVualeFUN.h"
#include "MyLuaVM.h"
//#include "LUAFUN.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CMyLuaVM::CMyLuaVM()
{

}

CMyLuaVM::~CMyLuaVM()
{

}
int CMyLuaVM::InitVM()
{
	L = lua_open();  /* create state */
	
	if ( !L )
	{
		return 1;
	}
	luaopen_base( L );
	luaopen_table( L );
	luaopen_math( L );
	luaopen_string( L );
//	luaopen_io( L );
	luaopen_os( L );
//	luaopen_package( L );
	RegisterVualeFunc(L);
	return 0;
}
int CMyLuaVM::Run( char *sFile )
{
	int error ;
	if ( L )
	{
		char szFilePath[512] = {0};
		char szdir[512] = {0};
		GetCurrentDirectoryA(512,szdir);
		sprintf(szFilePath,"%s\\%s",szdir,sFile);
		luaL_loadfile(L, szFilePath);
		error = lua_pcall(L, 0, LUA_MULTRET, 0);
	//error	= luaL_dofile( L,sFile );  
	}
	return error;
}
int CMyLuaVM::Close()
{
	if (L)
	{
		lua_close( L );
		L = NULL;
	}
	return 0;
}